package muilt;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.luculent.utils.WorkId;


public class RunHttp2 extends DriverProxyTest{
	@Test
	public void test() throws Exception{
		List<CompletableFuture> a = Lists.newArrayList(); 
		for(int i=0;i<30;i++){ 
			int ym = i;
			a.add(CompletableFuture.runAsync(() -> {
				//int ym = 0 ;//new Random().nextInt(100);
				
				String nHead = WorkId.sortUID();
				String url = "http://192.168.0.20:8082/cf_fpb/fupin/?iw-apikey=123&iw-cmd=workId&ym="+ym+"&requestId="+nHead;
				if(ym % 2 == 0)
					url = "http://192.168.31.131:8082/cf_fpb/fupin/?iw-apikey=123&iw-cmd=workId&ym="+ym+"&requestId="+nHead;
				
				lock.writeLock().lock();
				System.out.println("-----加锁------");
	        	requsets.add(Pair.of(nHead,ImmutableMap.of("key1", 1,"key2", nHead,"key3", 1))); 
	    		//从这里开始就有多线程问题了
	        	driver.get(url); 
	        	lock.writeLock().unlock();
	        	System.out.println("-----解锁------");
				String rst = "";
				try {
					//store.containsKey(nHead);
					//rst = store.poll(10, TimeUnit.SECONDS);
//					while(StringUtils.isBlank(store.get(nHead))){
//						Thread.sleep(1000);
//						System.out.println("---"+ym);
//					}
//					rst = store.get(nHead);
					rst = poll(nHead,1,10000000000L, TimeUnit.NANOSECONDS);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("S:"+nHead+"=="+rst);
				System.out.println("-----------");
				
			}));
		}
		CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()])).get();
	}
	
	
	public HttpClient getClient(){ 
		return  HttpClients.createDefault();
	}
	 
}
